import { _decorator, Component, Animation } from 'cc';
import { DIRECTION_ENUM, FSM_PARAMS_TYPE_ENUM,  EAT_STATUS_ENUM, } from '../../Enums';
import State from '../Base/State';
const { ccclass } = _decorator;
type ParamsValueType = boolean | number

export interface IParamsValue {
    type: FSM_PARAMS_TYPE_ENUM,
    value: ParamsValueType
}

export const getInitParamsNumber = () => {
    return {
        type: FSM_PARAMS_TYPE_ENUM.NUMBER,
        value: 0
    }
}

export const getInitParamsTrigger = () => {
    return {
        type: FSM_PARAMS_TYPE_ENUM.TRIGGER,
        value: false
    }
}

@ccclass('StateMachine')
export abstract class StateMachine extends Component {
    private _currentState: State = null
    params: Map<string, IParamsValue> = new Map()
    stateMachines: Map<string, State> = new Map()
    animationComponent: Animation
    direction: DIRECTION_ENUM = DIRECTION_ENUM.BOTTOM
    eat_food:EAT_STATUS_ENUM=EAT_STATUS_ENUM.EMPTY
    paramsName:string=""
    getParams(paramsName: string) {
        if (this.params.has(paramsName)) {
            return this.params.get(paramsName)?.value
        }
    }
    setEatFood(value:EAT_STATUS_ENUM){
        this.eat_food=value
    }
    setParams(paramsName: string, value: ParamsValueType) {
        if(this.paramsName=='dizzy'||this.paramsName=='win') return 
        if (this.params.has(paramsName)) {
            let name = paramsName == 'idle' ? this.direction : paramsName
            // 如果嘴里有食物
            if(this.eat_food!=EAT_STATUS_ENUM.EMPTY&&name!='dizzy'){
                name=name+"_eat"
            }
            if(this.params.get(name)){
                this.params.get(name).value = value
                this.run()
                this.resetTrigger()
                if (paramsName == 'idle') {
                   this.pause()
                }
                this.paramsName=paramsName
            }
        }
    }
    get currentState() {
        return this._currentState

    }

    set currentState(newVal: State) {
        if (newVal) {
            this._currentState = newVal
            this._currentState.run()

        }
    }
    setDirection(value: DIRECTION_ENUM) {
        this.direction = value
    }
    // 重置状态
    resetTrigger() {
        for (const [_, value] of this.params) {
            if (value.type == FSM_PARAMS_TYPE_ENUM.TRIGGER) {
                value.value = false
            }
        }
    }
    abstract init(): void
    abstract run(): void
    abstract pause(): void
}


